package com.authine.cloudpivot.ext.applicationservice;

import com.alibaba.cola.dto.SingleResponse;
import com.authine.cloudpivot.ext.service.OrderService;
import com.authine.cloudpivot.ext.service.impl.OrderServiceImpl;
import com.authine.cloudpivot.ext.utils.StringUtils;
import com.authine.hermes.app.launcher.service.ApplicationService;
import com.authine.mvp.app.launcher.domain.utills.ApplicationContextUtils;
import com.authine.mvp.app.launcher.rpc.SaveOrUpdateBO;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.support.TransactionTemplate;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.util.Map;

import static com.authine.hermes.app.launcher.service.ApplicationServiceFoundation.CUSTOM_SERVICE;

@Path("OrderController")
@Slf4j
public class OrderController extends ApplicationService {

    private OrderService orderService = OrderServiceImpl.getInstance();
    TransactionTemplate transactionTemplate = ApplicationContextUtils.getBean(TransactionTemplate.class);

    @Path("custCreateOrder")
    @POST
    @Operation(summary = "订单-提交客户订单", tags = CUSTOM_SERVICE, description = "")
    public SingleResponse custCreateOrder(SaveOrUpdateBO saveOrUpdateBO) {

        SingleResponse res = SingleResponse.buildFailure("-1", "失败");

        Map<String, Object> data = saveOrUpdateBO.getData();

        if (null == saveOrUpdateBO) {
            res.setErrMessage("参数问题");
            return res;
        }
        String erp_po = (String) data.get("erp_po");

        if (!StringUtils.between(erp_po, 0, 50)) {
            res.setErrMessage("ERP PO 单号不正确");
            return res;
        }
        String receiver = (String) data.get("receiver");

        if (StringUtils.isBlank(receiver) || !StringUtils.between(receiver, 1, 50)) {
            res.setErrMessage("收货人不正确");
            return res;
        }
        String phone = (String) data.get("phone");

        if (StringUtils.isBlank(phone) || !StringUtils.between(phone, 1, 50)) {
            res.setErrMessage("电话不正确");
            return res;
        }
        String address = (String) data.get("address");

        if (StringUtils.isBlank(address) || !StringUtils.between(address, 1, 500)) {
            res.setErrMessage("地址不正确");
            return res;
        }
        String postal = (String) data.get("postal");
        if (StringUtils.isBlank(postal)) {
            postal = "";
        } else if (!StringUtils.between(postal, 0, 50)) {
            res.setErrMessage("邮编不正确");
            return res;
        }

        //提交订单处理

        SingleResponse singleResponse = orderService.orderBeforCondition(data);

        if (singleResponse.isSuccess()) {
            SingleResponse execute = transactionTemplate.execute(transactionStatus -> orderService.submitOrder(data));
            log.info("====================\n====================={}", execute);
            return execute;

        } else {
            return singleResponse;
        }
    }

}
